#ifndef _SRC_MQTT_MQTTNODE_H_
#define _SRC_MQTT_MQTTNODE_H_

#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief mqtt 消息订阅处理器节点
 */
typedef struct {
    RecvHandler             handler;
    void*                   context;
    ListHead   linkedNode;
} MQTTSubHandlerNode;
/**
 * @brief MQTT 订阅节点
 * @param status 服务端是否已经响应
 */
typedef struct {
    char*                   topic;
    uint8_t                 status;
    uint16_t                packetId;
    uint8_t*                packet;
    uint32_t                length;
    uint64_t                sendAt;
    ListHead   linkedNode;
    ListHead   handleList;
} MQTTSubNode;
/**
 * @brief MQTT 发布节点
 */
typedef struct {
    uint16_t                packetId;
    uint8_t*                packet;
    uint32_t                length;
    uint64_t                sendAt;
    ListHead   linkedNode;
} MQTTPubNode;
/**
 * @brief 定义mqtt 处理数据
 */
typedef struct {
    MQTTProcessHandler handler;
    void *context;
} MQTTProcessData;

/**
 * @brief 定义mqtt 处理数据节点
 */
typedef struct {
    MQTTProcessData         processData;
    ListHead   linkedNode;
} MQTTProcessDataNode;

/**
 * @brief topic alias node
 */
typedef struct {
    char*                   topic;
    uint16_t                topicAlias;
    ListHead   linkedNode;
} MQTTTopicAliasNode;
/**
 * @brief 清空topoc alias
 * @param entity
 */
void remoteTopicAliasList(MQTTEntity *entity);
/**
 * @brief 销毁订阅队列
 * @param entity
 */
void destroyMqttSubList(MQTTEntity *entity);
/**
 * @brief 销毁发布队列
 * @param entity
 */
void destroyMqttPubList(MQTTEntity *entity);
/**
 * @brief 销毁数据队列
 * @param entity
 */
void destroyMqttProcessDataList(MQTTEntity *entity);
/**
 * @brief 订阅队列插入
 * @param entity
 * @param topic
 * @param handler
 * @param context
 * @return
 */
int32_t insertMqttSubList(MQTTEntity *entity, MQTTBuffer *topic, RecvHandler handler, void *context);
/**
 * @brief 从订阅队列中删除topic 处理器
 * @param entity
 * @param topic
 * @param handler
 */
void removeMqttSubListHandler(MQTTEntity *entity, MQTTBuffer *topic, RecvHandler handler);
/**
 * @brief 更新订阅队列节点数据
 * @param entity
 * @param topic     type为0时生效，其他时候填充任意值
 * @param packet    type为0时生效，其他时候填充任意值
 * @param len       type为0时生效，其他时候填充任意值
 * @param packetId  type为0时生效，其他时候填充任意值
 * @param status    type为1时生效，其他时候填充任意值
 * @param type 0表示修改packetId/packet/len,1表示修改result
 */
void updateMqttSubList(MQTTEntity *entity, MQTTBuffer *topic, uint16_t packetId, uint8_t *packet, uint32_t len, uint8_t status, uint8_t type);
/**
 * @brief 重置队列所有节点状态
 * @param entity
 */
void resetAllMqttSubListStatus(MQTTEntity *entity);
/**
 * @brief 插入发布队列中
 * @param entity
 * @param packet
 * @param len
 * @param packetId
 * @return
 */
int32_t insertMqttPubList(MQTTEntity *entity, uint8_t *packet, uint32_t len, uint16_t packetId);
/**
 * @brief 从订阅队列中删除topic
 * @param entity
 * @param topic
 */
void removeMqttSubList(MQTTEntity *entity, MQTTBuffer *topic);
/**
 * @brief 插入topoc alias
 * @param entity
 * @param topic
 * @param topicAlias
 * @param list
 * @return
 */
int32_t insertMqttTopicAliasList(MQTTEntity *entity, MQTTBuffer *topic, uint16_t topicAlias, ListHead *list);
/**
 * @brief 销毁订阅处理队列
 * @param entity
 * @param list
 */
void destroyMqttSubHandlerList(MQTTEntity *entity, ListHead *list);
/**
 * @brief 从发布队列中移除消息
 * @param entity
 * @param packetId
 */
void removeMqttPubList(MQTTEntity *entity, uint16_t packetId);
/**
 * @brief 添加订阅处理器
 * @param entity
 * @param dest
 * @param src
 * @param found
 */
void appendMqttHandlerList(MQTTEntity *entity, ListHead *dest, ListHead *src, uint8_t *found);

#ifdef __cplusplus
}
#endif

#endif //_SRC_MQTT_MQTTNODE_H_
